//
//  HeaderView.m
//  friendslist
//
//  Created by zhangyi on 15/11/29.
//  Copyright © 2015年 zhangyi. All rights reserved.
//

#import "HeaderView.h"
#import "Group.h"
@implementation HeaderView{
    UIButton *_btn;
    UILabel *_label;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+(instancetype)headerView:(UITableView*)tableView{
    static NSString *headerID = @"header";
    HeaderView *        header = [[HeaderView alloc] initWithReuseIdentifier:headerID];
    
    return header;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn setImage:[UIImage imageNamed:@"sanjiao.jpg"] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
        
        _btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        _btn.titleEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
        
        [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btn];
        
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        _label = label;
        [self addSubview:label];
    }
    return self;
}

-(void) layoutSubviews{
    [super layoutSubviews];
    _btn.frame = self.bounds;
    _label.frame = CGRectMake(self.frame.size.width-70, 0, 60, self.frame.size.height);
}

- (void) btnAction{
    self.group.show = !self.group.show;
    if ([self.delegate respondsToSelector:@selector(clickView)]) {
        [self.delegate clickView];
    }
}

-(void)setGroup:(Group *)group{
    _group = group;
    [_btn setTitle:group.name forState:UIControlStateNormal];
    _label.text = [NSString stringWithFormat:@"%ld/%ld",group.online,group.friends.count];
    _btn.imageView.transform = self.group.show?CGAffineTransformMakeRotation(M_PI_2):CGAffineTransformMakeRotation(0);
}

@end
